﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Entity;
using Ones.Dal.Repository.Interfaces;

namespace Ones.Dal.Repository.Impl
{
    public class AdvertisementDao : AbstractDao<Advertisement>, IAdvertisementDao
    {
        public override bool
        update(Advertisement advertisement)
        {
            try
            {
                var curr = getById(advertisement.advId);

                curr.advContId = advertisement.advContId;
                curr.advBlockId = advertisement.advBlockId;
                curr.advDateCreate = advertisement.advDateCreate;
                curr.advDateModify = advertisement.advDateModify;
                curr.advDateExpire = advertisement.advDateExpire;
                curr.advActive = advertisement.advActive;
                curr.advLink = advertisement.advLink;
                curr.advImageLink = advertisement.advImageLink;
                curr.advAltText = advertisement.advAltText;
                curr.advClickCount = advertisement.advClickCount;

                _ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public override Advertisement
        getById(object id)
        {
            return getSingle(i => i.advId == (int)id);
        }

        public List<Advertisement>
        getByContact(int advContId)
        {
            try
            {
                return getMultiple(i => i.advContId == advContId);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<Advertisement>
        getByBlock(string advBlockId)
        {
            try
            {
                return getMultiple(i => i.advBlockId.Equals(advBlockId));
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<Advertisement>
        getByActive(bool advActive)
        {
            try
            {
                return getMultiple(i => i.advActive == advActive);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<ViewAdvertisementDetail>
        getvw_AdvAdvCont()
        {
            try
            {
                return _ctx.ViewAdvertisementDetails.ToList<ViewAdvertisementDetail>();
            }
            catch (Exception)
            {
                return null;
            }
        }
    }

    #region IAdvertisementDao

    public interface IAdvertisementDao : IDao<Advertisement>
    {
        List<Advertisement> getByContact(int advContId);

        List<Advertisement> getByBlock(String advBlockId);

        List<Advertisement> getByActive(Boolean advActive);

        List<ViewAdvertisementDetail> getvw_AdvAdvCont();
    }
    #endregion
}